package ui;

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.DefaultCaret;

import model.Server.CloseAction;

public class ServerWindow {

	private JFrame frame;
	//private JButton sendButton;
	private JTextArea chatArea;
	//private JTextArea messageArea;

	private CloseAction ca;

	private String title;

	public ServerWindow(CloseAction ca, String title) {
		this.ca = ca;
		this.title = title;
		initialize();
	}

	public void show() {
		this.frame.setVisible(true);
	}

	public void makeExit() {
		ca.closeServer();
	}

	public void close() {
		frame.setVisible( false );
		frame.dispose();
	}

	private void initialize() {
		frame = new JFrame();
		frame.setResizable(false);
		frame.getContentPane().setBackground(Color.DARK_GRAY);
		frame.setBounds(100, 100, 789, 390);
		
		frame.setDefaultCloseOperation ( JFrame. DO_NOTHING_ON_CLOSE );
		WindowListener windowListener = new WindowAdapter()
		   {
				public void windowClosing ( WindowEvent w ) {
					close();
					makeExit();
		      }
		   };
		frame.addWindowListener( windowListener );
		
		frame.getContentPane().setLayout(null);
		frame.setTitle(this.title);
		
		this.chatArea = new JTextArea();
		this.chatArea.setEditable(false);
		this.chatArea.setBounds(10, 10, 760, 250);
		DefaultCaret chatAreaCaret = (DefaultCaret)chatArea.getCaret();
		chatAreaCaret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
		chatArea.setLineWrap(true);
		
		JScrollPane chatAreaScroll = new JScrollPane(chatArea);
		chatAreaScroll.setBounds(10, 10, 760, 250);
		chatAreaScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
		chatAreaScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		frame.getContentPane().add(chatAreaScroll);
		
//		this.messageArea = new JTextArea();
//		this.messageArea.setToolTipText("Escreva sua mensagem...");
//		this.messageArea.setBounds(10, 271, 671, 80);
//		DefaultCaret messageAreaCaret = (DefaultCaret)messageArea.getCaret();
//		messageAreaCaret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
//		messageArea.setLineWrap(true);
//		
//		JScrollPane messageAreaScroll = new JScrollPane(messageArea);
//		messageAreaScroll.setBounds(10, 271, 671, 80);
//		messageAreaScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
//		messageAreaScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//		
//		frame.getContentPane().add(messageAreaScroll);
//		
//		this.sendButton = new JButton("Send");
//		this.sendButton.addActionListener(this);
//		this.sendButton.setBounds(685, 271, 89, 80);
//		frame.getContentPane().add(this.sendButton);
	}

//	public void actionPerformed(ActionEvent e) {
//		try {
//		    if (e.getSource() == this.sendButton) {
//		    	this.chatArea.setText(this.chatArea.getText() + this.clientName + ": " + this.messageArea.getText() + "\n");
//		    	this.wl.writeMessage(this.messageArea.getText());
//		    	this.messageArea.setText("");
//		    }
//		} catch(Exception ex) {
//			ex.printStackTrace();
//		}
//	}

	public void showMessage(String message) {
		this.chatArea.setText(this.chatArea.getText() + message + "\n");
	}
}
